import {SysTagAdd, SysTagData, SysTagDetail, SysTagParams} from "@/pages/tag/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";
import {HttpResult, Page} from "@/services/typings";


export const sysTagPageList = async (sysTagParams: SysTagParams) => {
  return request<HttpResult<Page<SysTagData>>>('/api/tag/list', {
    method: 'POST',
    data: sysTagParams
  });
}


export const sysTagUpdate = async (sysTagAdd: SysTagAdd) => {
  return request<HttpResult<string>>('/api/tag/update', {
    method: 'POST',
    data: sysTagAdd
  });
}

export const sysTagAdd = async (sysTagAdd: SysTagAdd) => {
  return request<HttpResult<string>>('/api/tag/add', {
    method: 'POST',
    data: sysTagAdd
  });
}

export const sysTagRemove = async ( ids: (number | string)[] ) => {
  return request<HttpResult<string>>('/api/tag/remove', {
    method: 'POST',
    data: {
      ids
    }
  });
}

export const sysTagDetail = async (id: number) => {
  return request<HttpResult<SysTagDetail>>('/api/tag/detail', {
    method: 'POST',
    data: {
      id
    }
  });
}

export const sysTagImport = async (formData: FormData) => {
  return request<HttpResult<string>>('/api/tag/import', {
    method: 'POST',
    data: formData,
    requestType: "form"
  });
}


const sysTagExport = async (ids: (number | string)[]) => {
  return request<HttpResult<string>>('/api/tag/export', {
    method: 'POST',
    data: {ids},
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysTagExport = async (ids: (number | string)[]) => {
  await sysTagExport(ids).then(({data, response}) => {
    download(data, response)
  });
}


const sysTagExportTemplate = async () => {
  return request<HttpResult<string>>('/api/tag/export/template', {
    method: 'POST',
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysTagExportTemplate = async () => {
  await sysTagExportTemplate().then(({data, response}) => {
    download(data, response)
  });
}
